package com.restart.game.service.impl;

import com.restart.game.common.base.BaseServicesImpl;
import com.restart.game.common.base.MyBaseMapper;
import com.restart.game.entity.Info;
import com.restart.game.entity.InfoType;
import com.restart.game.mapper.InfoTypeMapper;
import com.restart.game.service.InfoTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author ReStartLin
 * @data 2018/12/12 19:42
 * @classDesc: 功能描述:
 */
@Service
public class InfoTypeServiceImpl extends BaseServicesImpl<InfoType,Integer> implements InfoTypeService {
    private final InfoTypeMapper mapper;

    @Autowired
    public InfoTypeServiceImpl(InfoTypeMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public MyBaseMapper<InfoType> getMapper() {
        return mapper;
    }

    @Override
    public Integer getId(String gameName) {
        Example example = new Example(InfoType.class);
        example.createCriteria().andEqualTo("type", gameName);
        List<InfoType> byExample = this.findByExample(example);
        if (byExample != null && byExample.size() > 0) {
            return byExample.get(0).getId();
        }
        return null;
    }

    @Override
    public void changeData(Integer typeId, boolean flag) {
        InfoType byId = this.findById(typeId);
        Integer count = byId.getCount();
        byId.setCount(--count);
        if (flag) {
            Integer submit = byId.getSubmit();
            byId.setSubmit(++submit);
        }
        this.updateByPrimaryKey(byId);
    }

}
